package com.wimcorp.magic.abilities;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.wimcorp.magic.abilities.trigger.AbilityTrigger;
import com.wimcorp.magic.behaviour.Behaviour;
import com.wimcorp.magic.behaviour.counter.Counter;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.exception.CastException;
import com.wimcorp.magic.input.Target;

/**
 * example {target:'ANY',locations:['BATTLEFIELD'],types :['CREATURE']
 * behaviors:,power :-2 toughness : -2}
 * 
 * target options : ANY,SELF,OPPONENT,THIS
 * 
 * types options : 'CREATURE'
 * 
 * location options : BATTLEFIELD
 * 
 * behaviors options : COUNTER
 * 
 * power :
 * 
 * toughness :
 * 
 * duration : PERMANENT,END_OF_TURN
 * 
 */
@Entity
@Table(name = "addcounter", schema = "abilities")
public class AddCounter extends Ability {

	@Transient
	private Set<Behaviour> counters = new HashSet<>();
	@Transient
	private Creature creature = null;

	private int power;
	private int toughness;

	protected AddCounter() {
	}

	public AddCounter(AbilityTrigger trigger) {
		super(trigger);
	}

	public AddCounter(AddCounter ability) {
		super(ability);
		this.power = ability.getPower();
		this.toughness = ability.getToughness();
	}

	@Override
	protected void executeAbility() throws CastException {
		creature = (Creature) getAbilityTarget();
		if (creature != null) {
			Counter counter = new Counter(power, toughness);
			creature.addBehaviour(counter);
			counters.add(counter);
			registerUndo();
		}
	}

	public void uncast() {
		if (creature != null) {
			for (Behaviour b : counters) {
				creature.removeBehaviour(b);
			}
		}
	}

	@Override
	public Ability clone() {
		return new AddCounter(this);
	}
	
	@Override
	public boolean isInputRequired(){
		if (Target.THIS.equals(getTarget())) {
			return false;
		}
		return true;
	}

	public int getPower() {
		return power;
	}

	public int getToughness() {
		return toughness;
	}

	public void setPower(int power) {
		this.power = power;
	}

	public void setToughness(int toughness) {
		this.toughness = toughness;
	}

}
